from owlready2 import *

go = get_ontology("http://purl.obolibrary.org/obo/go.owl").load()

print('1. Get the number of OWL Class in GO')
print('\t', list(default_world.sparql("""
        SELECT (COUNT(?x) AS ?nb)
        { ?x a owl:Class . }
""")))

# 预定义的前缀
# rdf: -> http://www.w3.org/1999/02/22-rdf-syntax-ns#
# rdfs: -> http://www.w3.org/2000/01/rdf-schema#
# owl: -> http://www.w3.org/2002/07/owl#
# xsd: -> http://www.w3.org/2001/XMLSchema#
# obo: -> http://purl.obolibrary.org/obo/
# owlready: -> http://www.lesfleursdunormal.fr/static/_downloads/owlready_ontology.owl#

print('2. Get the number of OWL Class in GO, by ISIRI')
print('\t', list(default_world.sparql("""
        SELECT (COUNT(?x) AS ?nb)
        { ?x a owl:Class . FILTER(ISIRI(?x)) }
""")))

print('3. Get the "mitochondrion inheritance" concept from GO')
print('\t', list(default_world.sparql("""
        SELECT ?x
        { ?x rdfs:label "mitochondrion inheritance" . }
""")))

print('4. get the superclasses of “mitochondrion inheritance”')
print('\t', list(default_world.sparql("""
        SELECT ?y
        { ?x rdfs:label "mitochondrion inheritance" .
            ?x rdfs:subClassOf* ?y }
""")))

print('5. search for individuals belonging to the class “mitochondrion inheritance” or one of its descendants')
print('\t', list(default_world.sparql("""
        SELECT ?y
        { ?x rdfs:label "mitochondrion inheritance" .
            ?y a/rdfs:subClassOf* ?x }
""")))

print('6. insert queries')
insertion = get_ontology("http://test.org/insertion.owl")
with insertion:
    default_world.sparql("""
        INSERT { ?x rdfs:label "héritage mitochondrial"@fr }
        WHERE  { ?x rdfs:label "mitochondrion inheritance" . }
        """)
    print('\tinsertion.label', insertion.label)

#TODO 继续完善



